package com.example.filesave;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.filesave.sql.Book;
import com.example.filesave.sql.MyOpenHelper;
import com.example.filesave.util.L;

import java.util.ArrayList;

public class ListView extends AppCompatActivity {

    MyOpenHelper openHelper;
    ArrayList<Book> books;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        openHelper = new MyOpenHelper(this, "BookStore.db", null, 1);
        Button btn = (Button) findViewById(R.id.create_database);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openHelper.getWritableDatabase();
                Toast.makeText(ListView.this, "表创建成功了", Toast.LENGTH_LONG).show();
            }
        });

        Button btn2 = (Button) findViewById(R.id.add_data);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();

                // 开始组装数据
                contentValues.put("name", "Thinking in java");
                contentValues.put("author", "外国人");
                contentValues.put("pages", 520);
                contentValues.put("price", 160.2);
                db.insert("book", null, contentValues);
                contentValues.clear();
                // 开始组装数据
                contentValues.put("name", "php Action");
                contentValues.put("author", "外国人");
                contentValues.put("pages", 521);
                contentValues.put("price", 160.2);
                db.insert("book", null, contentValues);
                Toast.makeText(ListView.this, "插入数据成功", Toast.LENGTH_LONG).show();
            }
        });

        Button btn3 = (Button) findViewById(R.id.update);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();

                contentValues.put("price", 10.333);
                db.update("book", contentValues, "name=?", new String[]{"php Action"});

                Toast.makeText(ListView.this, "修改成功", Toast.LENGTH_LONG).show();
            }
        });

        Button del = (Button) findViewById(R.id.delete);
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();

                db.delete("book", "pages>?", new String[]{"510"});

                Toast.makeText(ListView.this, "删除成功", Toast.LENGTH_LONG).show();
            }
        });

        Button select = (Button) findViewById(R.id.select);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();

                // 查询book中的所有的数据
                Cursor book = db.query("book", null, null, null, null, null, null);
                books = new ArrayList<>();
                if (book.moveToFirst()) {
                    do {
                        // 遍历
                        Integer id = Integer.valueOf(book.getString(book.getColumnIndex("id")));
                        String name = book.getString(book.getColumnIndex("name"));
                        String author = book.getString(book.getColumnIndex("author"));
                        Integer pages = Integer.valueOf(book.getString(book.getColumnIndex("pages")));
                        double price = Double.parseDouble(book.getString(book.getColumnIndex("price")));
                        L.d(id + "---" + name + "---" + author + "---" + pages + "---" + price);
                        books.add(new Book(id, name, pages, price, author));
                    } while (book.moveToNext());
                }
                android.widget.ListView lv = (android.widget.ListView) findViewById(R.id.lv);
                lv.setAdapter(new Myadapter());

//                Toast.makeText(ListView.this,"查找数据"+books.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }

    class Myadapter extends BaseAdapter {

        // 获取模型层有多少条数据
        @Override
        public int getCount() {
            return books.size();
        }

        //convertView 系统缓存的条目
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            L.d(position+"");
            Book book = books.get(position);
            /*TextView textView = new TextView(ListView.this);
            textView.setText(book.toString());
            textView.setTextSize(16);*/

            View inflate = null;
            if(convertView == null){
            // 把布局文件填充成view对象
                inflate= View.inflate(ListView.this, R.layout.item_listview, null);
            }else{
                // 把缓存放入View
                inflate = convertView;
            }


            TextView tv_name = (TextView) inflate.findViewById(R.id.item_name);
            TextView tv_author = (TextView) inflate.findViewById(R.id.item_author);
            TextView tv_price = (TextView) inflate.findViewById(R.id.item_price);
            TextView tv_pages = (TextView) inflate.findViewById(R.id.item_pages);
            tv_name.setText(book.getName());
            tv_author.setText(book.getAuthor());
            tv_price.setText(book.getPrice()+"");
            tv_pages.setText(book.getPages()+"");

            return inflate;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

    }
}
